package com.antcom.antread.common.core.utils.encryptUtil;


import com.antcom.antread.common.core.exception.CustomException;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * URL加解密
 *
 * @Author 中国蚂蚁技术有限公司
 * @create 2020/12/4 21:46
 */
public class URLUtil {

    /**
     * url加密
     *
     * @param param 参数
     * @return encode
     */
    public static String URLEn(String param) {
        try {
            String encode = URLEncoder.encode(param, "UTF-8");
            if (encode.contains("+") || encode.contains("%2F")) {
                encode = encode.replace("+", "%20");
                encode = encode.replace("%2F", "/");
            }
            return encode;
        } catch (UnsupportedEncodingException e) {
            throw new CustomException("URL编码失败");
        }
    }

    /**
     * url解密
     *
     * @param param 参数
     * @return decode
     */
    public static String URLDn(String param) {
        try {
            return URLDecoder.decode(param, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new CustomException("URL解码失败");
        }
    }
}
